package com.scalar.chiptrack.operations.shippingRequest.actionCommands;

import com.scalar.chiptrack.actioncommands.ActionCommand;
import com.scalar.chiptrack.operations.shippingRequest.forms.ShippingRequestForm;
import com.scalar.chiptrack.operations.shippingRequest.ShippingRequest;
import com.scalar.chiptrack.operations.shippingRequest.dao.ShippingRequestDAO;
import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.commons.Constants;
import com.scalar.chiptrack.db.DBTransaction;
import com.scalar.ScalarException;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;

/**
 * Created by ChandraSekahr.
 * User: Administrator
 * Date: Nov 21, 2005
 * Time: 11:18:25 AM
 */
public class ShippingRequestViewCommand extends ActionCommand
{
    private static final String ACTION_NAME = "/shippingRequest.do";
    public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response ) throws ScalarException
    {
        ShippingRequestForm shippingRequestForm     = ( ShippingRequestForm ) form;
        ShippingRequest shippingRequest             = shippingRequestForm.getShippingRequest();
        ActionForward actionForward                 = mapping.findForward( "view" );
        HttpSession mSession                        = request.getSession();
        User user                                   = ( User ) mSession.getAttribute ( SessionKeys.USER_KEY );
        DBTransaction dbTransaction                 = null;
        HashMap errors                              = new HashMap();
        HashMap info                                = new HashMap();
        int shippingRequestId                       = -1;
        request.setAttribute( INFO_MESSAGES, info );
        request.setAttribute( ERROR_MESSAGES, errors );

        if ( user == null )
        {
            mSession.setAttribute ( SessionKeys.SOURCE_PAGE_KEY,  ACTION_NAME );
            return ( mapping.findForward ( "login" ) );
        }

        try
        {
            try{
                shippingRequestId = Integer.parseInt( request.getParameter("shipReqId") );
            }catch( NumberFormatException nfe)
            {
                shippingRequestId = -1;
            }
            //TO LOAD THE SAVED SHIPPING REQUEST INFO
            shippingRequest  = ShippingRequestDAO.getShippingRequest( dbTransaction, shippingRequestId );

            shippingRequestForm.setShippingRequest( shippingRequest );
        }
        catch( Exception e )
        {
            errors.put( "Could not retrive the Shipping Request", null );
            e.printStackTrace();
            shippingRequestForm.setShippingRequest( new ShippingRequest() );
            //forwarding to the search page
            request.setAttribute( "vendorList", ShippingRequestDAO.loadClassStroeLocations( null )  );
            request.setAttribute( "partNumberList", ShippingRequestDAO.getProductList( Constants.LOCATION_TYPE_CLASS_STORE ) );
            request.setAttribute( "yearList", ShippingRequestDAO.getYearList() );
            request.setAttribute( "statusList", ShippingRequestDAO.getStatusList() );
            actionForward  = mapping.findForward( "search" );
        }
        request.setAttribute( INFO_MESSAGES, info );
        request.setAttribute( SessionKeys.ERROR_MESSAGES, errors );

        return actionForward;
    }
}
